package com.light.modules.admin.system.model;

import java.util.ArrayList;
import java.util.List;

import com.google.common.base.Objects;
import com.light.modules.admin.common.util.StringUtil;
import com.light.modules.admin.system.model.base.BaseSysMenuBtn;

/**
 * 菜单-按钮对应按钮实体
 * @author:zhangdb
 * @date:2017/12/5
 */
public class MenuBtn extends BaseSysMenuBtn<MenuBtn> {
	private static final long serialVersionUID = 1L;
	public static final MenuBtn me = new MenuBtn();

	public List<MenuBtn> findAll() {
		return find("select * from sys_menu_btn");
	}
	
	public List<MenuBtn> findByUser(SysUser user) {
		List<MenuBtn> result = new ArrayList<MenuBtn>();
		String roles = user.getStr("roles");
		if(StringUtil.isNotEmpty(roles)) {
			result = find("select distinct b.* from sys_role_btn a join sys_menu_btn b on a.btn_id=b.id where a.role_id in(" + roles + ")");
		}
		return result;
	}

	@Override
	public int hashCode() {
		return 31 * super.hashCode() + Objects.hashCode(getInt("id"));
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null || getClass() != obj.getClass()) {
			return false;
		}
		if (!super.equals(obj)) {
			return false;
		}
		final MenuBtn other = (MenuBtn) obj;
		return Objects.equal(this.getInt("id"), other.getInt("id"));
	}
}
